package com.sh.rbac.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sh.rbac.annotation.OperationLogger;
import com.sh.rbac.domain.dto.user.*;
import com.sh.rbac.domain.entity.User;
import com.sh.rbac.domain.enums.OperationTypeEnum;
import com.sh.rbac.result.Result;
import com.sh.rbac.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 *
 * @author lucky
 * @since 2024-04-04
 */

@Tag(name = "用户管理", description = "用户相关操作")
@RequestMapping("sys/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Operation(summary = "查询用户列表")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = User.class)))
    @GetMapping("list")
    @PreAuthorize("hasAuthority('sys:user:list')")
    public Result<List<User>> getList() {
        return Result.success(userService.list());
    }


    @Operation(summary = "分页查询用户信息")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = User.class)))
    @PostMapping("page/{current}/{limit}")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public Result<Page<User>> getPage(@PathVariable Long current, @PathVariable Long limit, @RequestBody UserQueryDTO userQueryDTO) {
        return Result.success(userService.getPage(current, limit, userQueryDTO));
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.ADD)
    @Operation(summary = "添加用户", description = "保存一个新用户")
    @ApiResponse(responseCode = "200", description = "成功保存用户")
    @PostMapping("save")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<Boolean> save(@RequestBody UserSaveDTO userSaveDTO) {
        return userService.save(userSaveDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新用户", description = "根据ID更新用户信息")
    @ApiResponse(responseCode = "200", description = "成功更新用户")
    @PutMapping("update")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<Boolean> update(@RequestBody UserUpdateDTO userUpdateDTO) {
        return userService.updateById(userUpdateDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.DELETE)
    @Operation(summary = "删除用户", description = "根据ID删除用户信息")
    @ApiResponse(responseCode = "200", description = "成功删除用户")
    @DeleteMapping("delete/{id}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<Boolean> delete(@PathVariable Long id) {
        return userService.removeById(id) ? Result.success() : Result.failure();
    }


    @Operation(summary = "根据ID查询用户")
    @ApiResponse(responseCode = "200", description = "返回查询结果", content = @Content(schema = @Schema(implementation = User.class)))
    @GetMapping("get/{id}")
    @PreAuthorize("hasAuthority('sys:user:get')")
    public Result<User> get(@PathVariable Long id) {
        return Result.success(userService.getById(id));
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.BATCH_DELETE)
    @Operation(summary = "批量删除用户", description = "批量删除用户信息")
    @ApiResponse(responseCode = "200", description = "成功删除用户")
    @DeleteMapping("batch-delete")
    @PreAuthorize("hasAuthority('sys:user:batch-delete')")
    public Result<Boolean> batchDelete(@RequestBody List<Long> ids) {
        return userService.removeBatchByIds(ids) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "更新用户个人信息", description = "根据ID更新用户个人信息")
    @ApiResponse(responseCode = "200", description = "成功更新用户")
    @PutMapping("update-profile")
    @PreAuthorize("hasAuthority('sys:user:update-profile')")
    public Result<Boolean> updateProfile(@RequestBody UserProfileDTO userProfileDTO) {
        return userService.updateProfile(userProfileDTO) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "修改密码")
    @ApiResponse(responseCode = "200", description = "修改成功")
    @PutMapping("update-password")
    @PreAuthorize("hasAuthority('sys:user:update-password')")
    public Result<Boolean> updatePassword(@RequestBody UserUpdatePwdDTO userUpdatePwdDTO) {
        return userService.updatePassword(userUpdatePwdDTO) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "重置密码")
    @ApiResponse(responseCode = "200", description = "重置密码成功")
    @GetMapping("reset-password/{id}")
    @PreAuthorize("hasAuthority('sys:user:reset-password')")
    public Result<Boolean> resetPassword(@PathVariable Long id) {
        return userService.resetPassword(id) ? Result.success() : Result.failure();
    }


    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.OTHER)
    @Operation(summary = "分配角色")
    @ApiResponse(responseCode = "200", description = "分配角色成功")
    @PostMapping("assign-role")
    @PreAuthorize("hasAuthority('sys:user:assign-role')")
    public Result<Boolean> assignRole(@RequestBody AssignRoleDTO assignRoleDTO) {
        return userService.assignRole(assignRoleDTO) ? Result.success() : Result.failure();
    }

    @OperationLogger(module = "系统管理->用户管理", type = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新用户状态", description = "根据ID更新用户状态")
    @ApiResponse(responseCode = "200", description = "成功更新用户状态")
    @PutMapping("update-status/{id}/{status}")
    @PreAuthorize("hasAuthority('sys:user:update-status')")
    public Result<Boolean> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        return userService.updateStatus(id, status) ? Result.success() : Result.failure();
    }


}
